package br.com.requisicao.compra.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.xhtmlrenderer.pdf.ITextRenderer;

/**
 * Classe respons�vel por retornar o arquivo PDF apartir de um c�digo html.
 * 
 */
public class ArquivoPDF implements ArquivoExportacao {

	/**
	 * Vari�vel que armazena o c�digo html
	 */
	private String htmlString;

	/**
	 * Construtor da classe
	 * 
	 * @param htmlString
	 *            C�digo html que ser� gerado o PDF
	 */
	public ArquivoPDF(String htmlString) {

		this.htmlString = htmlString;

	}

	/**
	 * Respons�vel por retornar o arquivo de exporta��o
	 * 
	 * @return ByteArrayOutputStream
	 * @throws Exception
	 *             Exception
	 */
	public Object getArquivo() throws Exception {
		try {
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			BufferedReader bufferedReader = new BufferedReader(
					new StringReader(htmlString));
			Tidy tidy = new Tidy();
			Document doc = tidy.parseDOM(bufferedReader, null);
			ITextRenderer iTextRenderer = new ITextRenderer();
			iTextRenderer.setDocument(doc, null);
			iTextRenderer.layout();
			iTextRenderer.createPDF(byteArrayOutputStream);
			return byteArrayOutputStream;
		} catch (Exception e) {
			return null;
		}
	}
}
